# Replication code for Weidmann&Toft
# "Promises and Pitfalls", CMPS 2010
# Nils B. Weidmann, March 2010

# set your working directory here
setwd("<working directory>")

# read data
bosnia <- read.table("replication_bosnia.txt", header=T)

# Figure 4 (p. 172)
plot(density(bosnia$elf91[bosnia$CONFLICT==1]), lty=1, lwd=3, xlim=c(0,1), main="", xlab="Ethnic diversity")
lines(density(bosnia$elf91[bosnia$CONFLICT==0]), lty=3, lwd=3)
t.test(bosnia$elf91[bosnia$CONFLICT==0], bosnia$elf91[bosnia$CONFLICT==1])

plot(density(bosnia$PREDMEAN[bosnia$CONFLICT==0]), lwd=3, lty=3, xlim=c(0,0.4), main="", xlab="Predicted violence level")
lines(density(bosnia$PREDMEAN[bosnia$CONFLICT==1]), lty=1, lwd=3)
t.test(bosnia$PREDMEAN[bosnia$CONFLICT==0], bosnia$PREDMEAN[bosnia$CONFLICT==1])

# Models shown in Table 1

# Model 1
m1a <- glm(as.factor(CONFLICT) ~  PREDMEAN + log(CELLCOUNT) + W1.CONFLICT, family=binomial(link=logit), data=bosnia)
summary(m1a)

# Model 2
m1b <- glm(as.factor(CONFLICT) ~  W1.PREDMEAN + log(CELLCOUNT) + W1.CONFLICT, family=binomial(link=logit), data=bosnia)
summary(m1b)

# Model 3
m1c <- glm(as.factor(CONFLICT) ~  elf91 + log(CELLCOUNT) + W1.CONFLICT, family=binomial(link=logit), data=bosnia)
summary(m1c)

# Model 4 
m1d <- glm(as.factor(CONFLICT) ~  PREDMEAN + elf91 + log(CELLCOUNT) + W1.CONFLICT, family=binomial(link=logit), data=bosnia)
summary(m1d)

# Model 5
m1e <- glm(as.factor(CONFLICT) ~  PREDMEAN + W1.PREDMEAN + elf91 + log(CELLCOUNT) + W1.CONFLICT, family=binomial(link=logit), data=bosnia)
summary(m1e)

#### Robustness checks ####

### Different aggregation schemes for spatial predictions
## Maximum
# Model 1
m1 <- glm(as.factor(CONFLICT) ~  PREDMAX + log(CELLCOUNT) + W1.CONFLICT, family=binomial(link=logit), data=bosnia)
summary(m1)

# Model 1
m1 <- glm(as.factor(CONFLICT) ~  W1.PREDMAX + log(CELLCOUNT) + W1.CONFLICT, family=binomial(link=logit), data=bosnia)
summary(m1)

# Model 3 
m3 <- glm(as.factor(CONFLICT) ~  PREDMAX + elf91 + log(CELLCOUNT) + W1.CONFLICT, family=binomial(link=logit), data=bosnia)
summary(m3)

# Model 4
m4 <- glm(as.factor(CONFLICT) ~  PREDMAX + W1.PREDMAX + elf91 + log(CELLCOUNT) + W1.CONFLICT, family=binomial(link=logit), data=bosnia)
summary(m4)


## Median
# Model 1
m1 <- glm(as.factor(CONFLICT) ~  PREDMED + log(CELLCOUNT) + W1.CONFLICT, family=binomial(link=logit), data=bosnia)
summary(m1)

# Model 1
m1 <- glm(as.factor(CONFLICT) ~  W1.PREDMED + log(CELLCOUNT) + W1.CONFLICT, family=binomial(link=logit), data=bosnia)
summary(m1)

# Model 3 
m3 <- glm(as.factor(CONFLICT) ~  PREDMED + elf91 + log(CELLCOUNT) + W1.CONFLICT, family=binomial(link=logit), data=bosnia)
summary(m3)

# Model 4
m4 <- glm(as.factor(CONFLICT) ~  PREDMED + W1.PREDMED + elf91 + log(CELLCOUNT) + W1.CONFLICT, family=binomial(link=logit), data=bosnia)
summary(m4)


### Test different weights matrics
## unstandardized neighbors 
# Model 1
m1 <- glm(as.factor(CONFLICT) ~  PREDMEAN + log(CELLCOUNT) + W2.CONFLICT, family=binomial(link=logit), data=bosnia)
summary(m1)

# Model 2
m1 <- glm(as.factor(CONFLICT) ~  W2.PREDMEAN + log(CELLCOUNT) + W2.CONFLICT, family=binomial(link=logit), data=bosnia)
summary(m1)

# Model 3
m2 <- glm(as.factor(CONFLICT) ~  elf91 + log(CELLCOUNT) + W2.CONFLICT, family=binomial(link=logit), data=bosnia)
summary(m2)

# Model 4 
m3 <- glm(as.factor(CONFLICT) ~  PREDMEAN + elf91 + log(CELLCOUNT) + W2.CONFLICT, family=binomial(link=logit), data=bosnia)
summary(m3)

# Model 5
m4 <- glm(as.factor(CONFLICT) ~  PREDMEAN + W2.PREDMEAN + elf91 + log(CELLCOUNT) + W1.CONFLICT, family=binomial(link=logit), data=bosnia)
summary(m4)

## inverse centroid distances 
# Model 1
m1 <- glm(as.factor(CONFLICT) ~  PREDMEAN + log(CELLCOUNT) + W3.CONFLICT, family=binomial(link=logit), data=bosnia)
summary(m1)

# Model 2
m2 <- glm(as.factor(CONFLICT) ~  W3.PREDMEAN + log(CELLCOUNT) + W3.CONFLICT, family=binomial(link=logit), data=bosnia)
summary(m2)

# Model 3
m3 <- glm(as.factor(CONFLICT) ~  elf91 + log(CELLCOUNT) + W3.CONFLICT, family=binomial(link=logit), data=bosnia)
summary(m3)

# Model 4 
m4 <- glm(as.factor(CONFLICT) ~  PREDMEAN + elf91 + log(CELLCOUNT) + W3.CONFLICT, family=binomial(link=logit), data=bosnia)
summary(m4)

# Model 5
m5 <- glm(as.factor(CONFLICT) ~  PREDMEAN + W3.PREDMEAN + elf91 + log(CELLCOUNT) + W3.CONFLICT, family=binomial(link=logit), data=bosnia)
summary(m5)

